﻿using fuzhu;
using SqlSugar;
using U8COWeb;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.AS;
using ZDEntity;

namespace Yonyou.U8.BO
{
    /// <summary>
    ///不能直接继承，因为调用co时，根据AS_Department类直接转换
    /// </summary>
    public class DepartmentVM 
    {
        //当前的数据库连接字符串
        public string conStr { get; set; }
        public string xmlConfig { get; set; }
        public static string cDatabase = "";

        public AS_Department entity { get; set; }

        public DepartmentVM()
        {
            //conStr
            conStr = CanShu.conStr;
            //初始化定义xmlconfig
            xmlConfig = XmlOperation.GetConfigXML();
        }

        /// <summary>
        /// 导入U8，新增或修改
        /// </summary>
        /// <returns></returns>
        public  RetModel2 Import()
        {
               
            try
            {
                Department department = GetByDepcode();
                if (department ==null)
                {
                    if (string.IsNullOrEmpty(entity.dDepBeginDate))
                    {
                        entity.dDepBeginDate = DateTime.Today.ToString("yyyy-MM-dd");
                    }
                    //新增出来
                   RetModel2 RET = Add();
                    insertResult insert = new insertResult() {
                        cReCode = RET.code.ToString(),
                        Result = RET.message,
                        cVouCode = entity.cDepCode

                    };
                    List<insertResult> lst  = new List<insertResult>();
                    lst.Add(insert);
                    RET.data = lst;
                    return RET;
                }
                else
                {
                    entity.dDepBeginDate  =DbHelper.GetDbDate(department.dDepBeginDate).ToString("yyyy-MM-dd");
                    //更新
                    RetModel2 RET = Update();
                    insertResult insert = new insertResult()
                    {
                        cReCode = RET.code.ToString(),
                        Result = RET.message,
                        cVouCode = entity.cDepCode

                    };
                    List<insertResult> lst = new List<insertResult>();
                    lst.Add(insert);
                    RET.data = lst;
                    return RET;
                }               
               
            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }



        }

        public RetModel2 Add()
        {
            try
            {
                string cerr;
                string cid;
                 //末级编码系统自动处理了
                if (DepartmentDALCO.AddDepartment(this.entity, xmlConfig, out cerr, out cid))
                {
                    return DBService.ReOKObject("新增完成");
                }
                else
                {
                    return DBService.ReErrObject(cerr);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }

        /// <summary>
        ///  变更
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public RetModel2  Update()
        {
            try
            {
                string cerr;
                string cid; 
                if (DepartmentDALCO.ModifyDepartment(this.entity, xmlConfig, out cerr, out cid))
                {
                    return DBService.ReOKObject("修改完成");
                }
                else
                {
                    return DBService.ReErrObject(cerr);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
             
        }

       
        /// <summary>
        /// 检查部门是否存在，返回U8部门
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Department GetByDepcode()
        {
            if (entity.cDepCode == null)
            {
                return null;
            }
            
            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            { 
                
                Department department = db.Queryable<Department>().Where(x => x.cDepCode == entity.cDepCode).First();
                 return department; 
            } 
        
        }
        /// <summary>
        /// 检查部门是否存在
        /// </summary>
        /// <param name="cDepCode"></param>
        /// <returns></returns>
        public bool Exists(string cDepCode)
        {
           
            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = CanShu.conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {

               bool bExists = db.Queryable<Department>().Where(x => x.cDepCode == cDepCode).Any();
               
               return bExists;
            }
        }


        /// <summary>
        /// 检查部门是否存在
        /// </summary>
        /// <param name="cDepCode"></param>
        /// <returns></returns>
        public bool bEndDepart(string cDepCode)
        {

            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = CanShu.conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {

                bool bExists = db.Queryable<Department>().Where(x => x.cDepCode == cDepCode && x.bDepEnd).Any();

                return bExists;
            }
        }

        /// <summary>
        /// 读取部门信息，取末级的部门
        /// </summary>
        /// <param name="cwhcode">编码或者名称</param>
        /// <returns></returns>
        public RetModel2 GetEndList()
        {
            try
            {
                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {

                    List<Department> lst = db.Queryable<Department>().Where(x => (x.bDepEnd)).ToList();
                    if (lst != null)
                    { 
                        return DBService.ReOKObject(lst); 
                    }
                    else
                    {
                        return DBService.ReErrObject("查询不到部门！");
                    }
                }
            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }

        }
    }
}
